package com.xt.shop.Controller;

import com.xt.shop.Model.DiscountInfo;
import com.xt.shop.Model.RestBean;
import com.xt.shop.service.impl.DiscountInfoServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api/discount")
public class DiscountController {
    @Resource
    DiscountInfoServiceImpl discountInfoService;

    @RequestMapping("/insert")
    public RestBean insert(@RequestBody DiscountInfo discountInfo){
        String uuid = UUID.randomUUID().toString();
        discountInfo.setDiscountId(uuid);
        discountInfo.setState(1);
        discountInfo.setInputTime(new Date());
        if (discountInfoService.save(discountInfo)) {
            return RestBean.success(discountInfo);
        }
        return RestBean.failure(500,"新建失败");
    }

    @RequestMapping("/select/{page}/{item}")
    public RestBean list(@PathVariable int page,@PathVariable int item){
        return RestBean.success(discountInfoService.queryList(page,item));
    }

    @DeleteMapping("/del/{shopId}")
    public RestBean del(@PathVariable String  shopId){
        DiscountInfo byId = discountInfoService.getById(shopId);
        if (byId != null){
            byId.setState(0);
            if (discountInfoService.updateById(byId)) {
                return RestBean.success("删除成功");
            }
            return RestBean.failure(500,"删除异常");
        }
        return RestBean.failure(400,"参数异常");
    }

}
